package cn.chenmanman.system.service.impl;

import cn.chenmanman.model.system.SysMenu;
import cn.chenmanman.model.system.SysRoleMenu;
import cn.chenmanman.model.vo.AssginMenuVo;
import cn.chenmanman.model.vo.RouterVo;
import cn.chenmanman.system.mapper.SysMenuMapper;
import cn.chenmanman.system.mapper.SysRoleMenuMapper;
import cn.chenmanman.system.service.SysMenuService;
import cn.chenmanman.system.utils.MenuHelper;
import cn.chenmanman.system.utils.RouterHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService{

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        // 查到所有的菜单
        List<SysMenu> sysMenus = baseMapper.selectList(null);
        return MenuHelper.buildTrees(sysMenus);
    }

    @Override
    public SysMenu findNode(String menuId) {
        return null;
    }

    @Override
    public List<SysMenu> getMenuByRoleId(String roleId) {
        // 查出status=1的所有菜单
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        List<SysMenu> sysMenuList = baseMapper.selectList(wrapper);

        // 查出角色已分配的菜单
        QueryWrapper<SysRoleMenu> roleMenuAssginWrapper = new QueryWrapper<>();
        roleMenuAssginWrapper.eq("role_id", roleId);
        List<SysRoleMenu> assginMenus = sysRoleMenuMapper.selectList(roleMenuAssginWrapper);
        List<String> assginMenuList = assginMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());

        // 比对
        for (SysMenu menu:
             sysMenuList) {
            menu.setSelect(assginMenuList.contains(menu.getId()));
        }

        return MenuHelper.buildTrees(sysMenuList);
    }

    @Override
    public void assginMenu(AssginMenuVo assginMenuVo) {
        // 删除之前为角色所分配的菜单
        QueryWrapper<SysRoleMenu> roleMenuWrapper = new QueryWrapper<>();
        roleMenuWrapper.eq("role_id", assginMenuVo.getRoleId());
        sysRoleMenuMapper.delete(roleMenuWrapper);

        // 添加新分配的菜单
        for (String menuId:
             assginMenuVo.getMenuIdList()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
    }

    @Override
    public List<RouterVo> getUserMenuList(String userId) {
        List<SysMenu> sysMenuList = null;
        if("1".equals(userId)) {
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("status", "1");
            wrapper.orderByAsc("sort_value");
            sysMenuList = baseMapper.selectList(wrapper);
        } else {
            sysMenuList = baseMapper.findMenuListByUserId(userId);
        }
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTrees(sysMenuList);

        return RouterHelper.buildRouters(sysMenuTreeList);
    }

    @Override
    public List<String> getUserButtonList(String userId) {
        List<SysMenu> sysMenuList = null;
        if("1".equals(userId)) {
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("status", "1");
            sysMenuList = baseMapper.selectList(wrapper);
        } else {
            sysMenuList = baseMapper.findMenuListByUserId(userId);
        }
        List<String> perms = new ArrayList<>();
        for(SysMenu sysMenu: sysMenuList){
            if (sysMenu.getType() == 2) {
                perms.add(sysMenu.getPerms());
            }
        }

        return perms;
    }
}
